package com.gitee.slowcreator.http.enhance.factory;

import com.gitee.slowcreator.http.enhance.HttpRequestHandle;
import com.gitee.slowcreator.http.enhance.scanner.HttpInterfaceScanner;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zxw
 * @date 2023/1/7 下午9:34
 */
@Slf4j
public class HttpClientContext {

    @SuppressWarnings("unchecked")
    public static <T> T getHttpClient(Class<T> type) {

        if (type == null) {
            log.error("需要代理的类不能为空！");
            throw new IllegalArgumentException("代理参数不能为空！");
        }

        HttpProxyFactory<T> httpProxyFactory = (HttpProxyFactory<T>) HttpInterfaceScanner.get(type);
        if (httpProxyFactory == null) {
            log.error("class [{}] not exist", type.getName());
            throw new IllegalArgumentException("未找到对应的proxy factory！");
        }
        HttpRequestHandle<T> httpRequestHandle = new HttpRequestHandle<>(type);
        return httpProxyFactory.newInstance(httpRequestHandle);
    }
}
